//
//  ViewController.m
//  网络图片浏览器
//
//  Created by 张钧 on 2017/5/4.
//  Copyright © 2017年 JinYun. All rights reserved.
//

#import "ViewController.h"
#import "Layout.h"
#import "Model.h"
#import "CollectionViewCell.h"
#import "SecondVC.h"
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
{
    UICollectionView * _collection;
    NSMutableArray * _showArr;
}
@property(nonatomic,strong)NSMutableArray * dataArr;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //我是付洪恩
    
    
    
    self.title=@"瀑布流";
    //右按钮
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"ADD" style:UIBarButtonItemStyleDone target:self action:@selector(add)];
    //获取plist文件内容
    NSString * path=[[NSBundle mainBundle]pathForResource:@"photos" ofType:@"plist"];
    NSDictionary * dic=[NSDictionary dictionaryWithContentsOfFile:path];
    NSArray * arr=dic[@"content"];
    _showArr=[NSMutableArray array];
    _dataArr =[NSMutableArray array];
    for (NSDictionary * dic2 in arr) {
        Model * model=[[Model alloc]init];
        [model setValuesForKeysWithDictionary:dic2];
        [_dataArr addObject:model];
    }
    //初始化布局

    Layout * layout=[[Layout alloc]init];
    [layout backHeight:^CGFloat(NSIndexPath *indexPath, CGFloat width) {
        Model * model=_showArr[indexPath.row];
        CGFloat height=(width/model.width) * model.height;
        return height;
    }];
    
    
    //初始化网格视图
    _collection=[[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:layout];
    _collection.backgroundColor=[UIColor yellowColor];
    //签订协议
    _collection.delegate=self;
    _collection.dataSource=self;
    [_collection registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"cell"];

    [self.view addSubview:_collection];
    
}
-(void)add
{
    //判断数组是否小于数据数组  小于则添加
    if (_showArr.count<self.dataArr.count) {
        [_showArr addObject:self.dataArr[_showArr.count]];
        [_collection reloadData];
    }
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _showArr.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CollectionViewCell * cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    Model * model=_showArr[indexPath.item];
    cell.model=model;
    cell.backgroundColor=[UIColor cyanColor];
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    SecondVC * second=[[SecondVC alloc]init];
    Model * model=_showArr[indexPath.row];
    second.model=model;
    [self.navigationController pushViewController:second animated:NO];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
